package com.algo.ext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * BeanDefinitionRegistryPostProcessor 继承自 BeanFactoryPostProcessor
 * BeanDefinitionRegistryPostProcessor
 *      AnnotationConfigApplicationContext初始化 -> refresh -> invokeBeanFactoryPostProcessors
 *                      -> postProcessBeanDefinitionRegistry -> postProcessBeanFactory
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor: 容器中beanDefinitionCount为：" + registry.getBeanDefinitionCount());

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(UserService.class);
        // registry.registerBeanDefinition("userService2", beanDefinitionBuilder.getBeanDefinition());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor: 容器中beanDefinitionCount为：" + beanFactory.getBeanDefinitionCount());
    }

}
